/*
 * @Descripttion: 
 * @version: 
 * @Author: sueRimn
 * @Date: 2020-06-10 11:37:03
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-06-15 16:11:18
 */
// pages/examResult/examResult.js
import urlconf from '../../utils/urlconf.js'
import httpRequest from '../../utils/request.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        examId: '',
        examResult: '',
        resultOptionList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        let { examId, nums, lastPage, name } = options
        this.setData({
            examId,
            nums,
            lastPage,
            name
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let resultOptionList = []
        let nums = this.data.nums
        for (let i = 0; i < nums; i++) {
            resultOptionList.push({
                num: i + 1,
                status: 3,
                style: ''
            })
        }
        this.setData({
            resultOptionList
        })
        let examId = this.data.examId
        httpRequest(urlconf.getRes, { examId }, 'get').then(res => {
            console.log('查询结果result', res);
            if (res.code == 1) {
                this.setData({
                    examResult: res.data
                })
                resultOptionList.map((item, i) => {
                    for (let j in res.data.allNums) {
                        if (item.num == res.data.allNums[j].num) {
                            item.status = res.data.allNums[j].mark
                            switch (res.data.allNums[j].mark) {
                                case 1:
                                    item.style = 'correctColor'
                                    break
                                case 2:
                                    item.style = 'wrongColor'
                                    break
                                default:
                                    item.style = 'noColor'
                                    break
                            }
                        }
                    }
                })
                this.setData({
                    resultOptionList
                })
            }
        })
    },
    reStart() {
        let examId = Number(this.data.examId)
        wx.removeStorageSync('historyIndex')
        httpRequest(urlconf.clear, { examId }, 'post').then(res => {
            console.log('重新评估result', res);

            if (res.code == 1) {
                if (this.data.lastPage == 'card') {
                    wx.navigateBack({
                        delta: 3
                    });
                } else {
                    wx.navigateBack({
                        delta: 1
                    });
                }
            }
        })
    },
    LookQuestion(e) {
        let index = e.currentTarget.dataset.index;
        console.log(index);
        wx.setStorageSync('historyIndex', index)
        console.log(this.data.lastPage);
        if (this.data.lastPage == 'card') {
            wx.navigateBack({
                delta: 2
            });
        } else {
            wx.navigateTo({
                url: `../question/question?examId=${this.data.examId}&&nums=${this.data.nums}&&name=${this.data.name}`
            })
        }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let shareImgUrl = app.globalData.shareImg
        return {
            imageUrl: shareImgUrl,
            path: '/pages/index/index'
        };
    }
})